package com.ysg.widget.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.ysg.R;

/**
 * 选中或未选中按钮
 */
public class YSelectButton extends LinearLayout {
    public interface OnSelectButtonListener{
        void onClickListener();
    }
    OnSelectButtonListener listener;
    public void setOnSelectButtonListener(OnSelectButtonListener listener){
        this.listener = listener;
    }


    private Button radioButton;
    boolean checked;
    int checkShapeId;
    int uncheckShapeId;
    int checkTextColor;
    int uncheckTextColor;
    String checkContent;
    String uncheckContent;

    public YSelectButton(Context context) {
        super(context);
    }

    public YSelectButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        View view = View.inflate(context, R.layout.widget_select_button, this);
        radioButton = view.findViewById(R.id.radioButton);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YSelectButton);
        checked = array.getBoolean(R.styleable.YSelectButton_check, false);
        checkShapeId = array.getResourceId(R.styleable.YSelectButton_checkShape, R.drawable.shape_radio_button_uncheck);
        checkTextColor = array.getColor(R.styleable.YSelectButton_checkTextColor, getResources().getColor(R.color.grayLightColor));
        checkContent = array.getString(R.styleable.YSelectButton_checkContent);

        uncheckShapeId = array.getResourceId(R.styleable.YSelectButton_uncheckShape, R.drawable.shape_radio_button_checked);
        uncheckTextColor = array.getColor(R.styleable.YSelectButton_uncheckTextColor, getResources().getColor(R.color.white));
        uncheckContent = array.getString(R.styleable.YSelectButton_uncheckContent);

        setUI();

        radioButton.setOnClickListener(view1 -> {
            if (listener != null){
                listener.onClickListener();
            }
        });
    }

    private void setUI(){
        if (checked){
            radioButton.setText(checkContent);
            radioButton.setTextColor(checkTextColor);
            radioButton.setBackgroundResource(checkShapeId);
        }else{
            radioButton.setText(uncheckContent);
            radioButton.setTextColor(uncheckTextColor);
            radioButton.setBackgroundResource(uncheckShapeId);
        }
    }

    public boolean getChecked(){
        return checked;
    }

    public void setCheck(boolean checked){
        this.checked = checked;
        setUI();
    }







}
